<!-- 票券管理 -->
<template>
  <el-form ref="ruleFormRef" :model="ruleForm" inline class="ruleForm">
    <el-form-item label="票券名称：" prop="deviceCode">
      <el-input
        v-model="ruleForm.queryRo.couponName"
        style="width: 150px"
        clearable
        placeholder="请输入票券名称"
      />
    </el-form-item>
    <el-form-item label="用户名称：" prop="deviceCode">
      <el-input
        v-model="ruleForm.queryRo.couponName"
        style="width: 150px"
        clearable
        placeholder="请输入票券名称"
      />
    </el-form-item>
    <el-form-item label="使用时间：" >
      <el-date-picker
        v-model="time"
        type="daterange"
        style="width: 200px"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD"
      />
    </el-form-item>

    <el-form-item label="票券使用状态：" prop="statusList">
      <el-select v-model="ruleForm.queryRo.status" clearable style="width: 150px">
        <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
        />
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableList" style="width: 100%" v-loading="loading">
    <el-table-column prop="code" label="卡券唯一表编号" align="center" />
    <el-table-column prop="couponName" label="票券名称" align="center" >
      <template #default="scope">
        <span >{{ scope.row.couponName}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="discount" label="优惠金额" align="center" />
    <el-table-column prop="status" label="使用状态" align="center">
      <template #default="scope">
        <span >{{ statusNum(scope.row.status)}}</span>
      </template>
    </el-table-column>
<!--    <el-table-column prop="startTime" label="使用开始时间" align="center">-->
<!--      <template #default="scope">-->
<!--        <span >{{ scope.row.startTime}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
<!--    <el-table-column prop="endTime" label="使用结束时间" align="center">-->
<!--      <template #default="scope">-->
<!--        <span >{{ scope.row.endTime}}</span>-->
<!--      </template>-->
<!--    </el-table-column>-->
    <el-table-column prop="orderNo" label="订单id" align="center" />
    <el-table-column prop="useTime" label="使用时间" align="center" />
<!--    <el-table-column prop="stationType" label="站点类型" align="center" />-->
    <el-table-column prop="adminName" label="发送者名称" align="center" />
    <el-table-column prop="nullifyDesc" label="作废原因" align="center" />
    <el-table-column prop="nullifyTime" label="作废时间" align="center" />
    <el-table-column prop="getType" label="获取方式" align="center" >
      <template #default="scope">
        <span >{{ getTypeNum(scope.row.getType) }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button link @click="openDialog(scope.row)" type="primary">查看详情</el-button>
      </template>
    </el-table-column>
  </el-table>

  <footer class="pagination">
    <el-pagination
      v-model:currentPage="ruleForm.current"
      v-model:page-size="ruleForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      small
    />
  </footer>

  <CouponDetailDialog
      v-model:show="showDialog"
      :detail="detail"></CouponDetailDialog>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import CouponDetailDialog from './components/couponDetailDialog.vue'
import api from '@/api/apis/couponApi'
const ruleForm = reactive({
  queryRo:{
    userId:"",
    couponName:"",
    userName:"",
    startTime:"",
    endTime:"",
    status:""
  },
  current: 1,
  pageSize: 50,
  searchCount: true
})
const time=ref<any>([])
//总数
const total = ref(0)
//搜索
const search = () => {
  ruleForm.current = 1
  getTableList()
}

const getTypeNum = (value: string) => {
  let obj: any = {
    '00': '分享获得',
  }
  return obj[value]
}

const statusNum = (value: number) => {
  let obj: any = {
    1: '未使用',
    2: '已使用',
    3: '已作废',
  }
  return obj[value]
}
const stationTypeNum = (value: number) => {
  let obj: any = {
    1: '全部站点',
    2: '部分站点',
  }
  return obj[value]
}
const statusList =[
  {name:"未使用",value:1},
  {name:"已使用",value:2},
  {name:"已作废",value:3},
]


//表格数据
const tableList = ref([])
//加载
const loading = ref(false)
onMounted(()=>{
  getTableList()
})
//获取表格数据
const getTableList = async () => {
  loading.value = true
  try {
    let obj = { ...ruleForm }
    obj.queryRo.startTime =time.value?time.value[0]:undefined
    obj.queryRo.endTime =time.value?time.value[1]:undefined
    let {data} = await api.couponListPage(obj)
    tableList.value= data.data.rows
    total.value= Number(data.data.total)

  } catch (error) {
    loading.value = false
  }
  loading.value = false
}
// //改变每页条数
const handleSizeChange = (val: number) => {
  ruleForm.current = 1
  ruleForm.pageSize = val
  getTableList()
}
//改变当前页数
const handleCurrentChange = (val: number) => {
  ruleForm.current = val
  getTableList()
}

//弹框开关
const showDialog = ref(false)
//详情id
const detail = ref()
//打开详情弹框
const openDialog = (val: string) => {
  detail.value = val
  showDialog.value = true
}
</script>

<style scoped lang="scss">
footer {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
